import mitt, { Handler } from 'mitt'
const emitter = mitt()

export const emitRevalidateTeleportTarget = (targetID: string) => {
    if (document.getElementById(targetID) == null) {
        throw Error("Teleport target does not exist on dom")
    }
    emitter.emit('revalidate-teleport-target', targetID)
}

export const onRevalidateTeleportTarget = (f: (targetID: string) => void) => {
    emitter.on('revalidate-teleport-target', f as Handler)
}

export const emitRemoveComponent = (componentName: string) => {
    emitter.emit('remove-component', componentName)
}

export const setHandleRemoveComponent = (f: (componentName: string) => void) => {
    emitter.on('remove-component', f as Handler)
}

export const reshowRemovedComponent = (componentName: string) => {
    emitter.emit('reshow-component', componentName)
}

export const setOnReshowRemovedComponent = (f: (componentName: string) => void) => {
    emitter.on('reshow-component', f as Handler)
}
